package com.df.ez_game.server;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 轮询负载均衡策略
 */
public class RoundRobinStrategy implements LoadBalanceStrategy {
    private final AtomicInteger index = new AtomicInteger(0);

    @Override
    public BrokerInfo selectBroker(List<BrokerInfo> brokers) {
        if (brokers == null || brokers.isEmpty()) {
            return null;
        }
        
        int nextIndex = index.getAndIncrement() % brokers.size();
        if (nextIndex < 0) {
            nextIndex = 0;
            index.set(0);
        }
        
        return brokers.get(nextIndex);
    }
}
